package com.owen.server.impl;

import com.owen.dao.UserDao;
import com.owen.pojo.User;
import com.owen.server.UserServer;
import com.owen.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServerImpl implements UserServer {

    @Autowired
    private UserDao userDao;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public User login(String username, String password) {
        return userDao.login(username,password);
    }

    @Override
    public List<User> find() {
        List<User> users = (List<User>)redisUtil.get("findUser");
        if(users == null){
            users = userDao.find();
            redisUtil.set("findUser", users);
        }
        return users;
    }

    @Override
    public Integer disable(Integer id) {
        Integer disable = userDao.disable(id);
        redisUtil.del("findUser");
        return disable;
    }

    @Override
    public Integer enable(Integer id) {
        Integer disable = userDao.enable(id);
        redisUtil.del("findUser");
        return disable;
    }

    @Override
    public Integer userDelete(Integer id) {
        Integer disable = userDao.userDelete(id);
        redisUtil.del("findUser");
        return disable;
    }

    @Override
    public List<User> findByName(User user) {
        return userDao.findByName(user);
    }

    @Override
    public Integer updatepassword(User user) {
        Integer disable = userDao.updatepassword(user);
        redisUtil.del("findUser");
        return disable;
    }

    @Override
    public Integer addUser(User user) {
        Integer disable = userDao.addUser(user);
        redisUtil.del("findUser");
        return disable;
    }

    @Override
    public Integer deleteBatch(Integer[] ids) {
        Integer disable = userDao.deleteBatch(ids);
        redisUtil.del("findUser");
        return disable;
    }

}
